package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.entity.WalletRepayment;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletApplyDebitContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletDebitContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

public class WalletApplyDebitPresenter extends BasePresenter<WalletApplyDebitContract.Model, WalletApplyDebitContract.View> {

    @Inject
    public WalletApplyDebitPresenter(WalletApplyDebitContract.Model model, WalletApplyDebitContract.View rootView) {
        super(model, rootView);
    }


    public void getDebitPeriodsSuccess(TreeMap<String, Object> map) {

        addSubscribe(mModel.getDebitPeriodsSuccess(map)
                .compose(RxUtils.<List<WalletDebit>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<WalletDebit>>(mView, true) {
                    @Override
                    public void onNext(List<WalletDebit> data) {
                        mView.onGetDebitPeriodsSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onGetDebitPeriodsFailed();
                        super.onError(e);
                    }
                }));
    }

    public void getPurposeList() {

        addSubscribe(mModel.getPurposeList()
                .compose(RxUtils.<List<String>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<String>>(mView, true) {
                    @Override
                    public void onNext(List<String> data) {
                        mView.onGetPurposeList(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getDebitCredit(TreeMap<String, Object> map) {

        addSubscribe(mModel.getDebitCredit(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView, true) {
                    @Override
                    public void onNext(String data) {
                        mView.onGetDebitCredit(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getRepaymentScheduleList(TreeMap<String, Object> map) {

        addSubscribe(mModel.getRepaymentScheduleList(map)
                .compose(RxUtils.<List<WalletRepayment>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<WalletRepayment>>(mView, true) {
                    @Override
                    public void onNext(List<WalletRepayment> data) {
                        mView.onGetRepaymentScheduleListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException){
                            mView.onGetRepaymentScheduleListFailed(e.getMessage());
                        }
                        super.onError(e);
                    }
                }));
    }


    public void applyDebit(TreeMap<String, Object> map) {

        addSubscribe(mModel.applyDebit(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView, true) {
                    @Override
                    public void onNext(String data) {
                        mView.onApplyDebitSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }





}




